#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e6 + 10;

int n, k;
int hh, tt;
int a[N], q[N];

int main()
{
    cin >> n >> k;
    for (int i = 0;i < n;i++) cin >> a[i];

    hh = 0, tt = -1;
    for (int i = 0;i < n;i++)
    {
        if (hh <= tt && i - k + 1 > q[hh]) hh++;
        while (hh <= tt && a[i] <= a[q[tt]]) tt--;
        q[++tt] = i;
        if (i + 1 >= k) printf("%d ", a[q[hh]]);
    }

    cout << endl;

    hh = 0, tt = -1;
    for (int i = 0;i < n;i++)
    {
        if (hh <= tt && i - k + 1 > q[hh]) hh++;
        while (hh <= tt && a[i] >= a[q[tt]]) tt--;
        q[++tt] = i;
        if (i + 1 >= k) printf("%d ", a[q[hh]]);
    }

    cout << endl;
}